<template>
  <div class="app-container" style="margin-top: -35px;">
    <el-table  v-loading="loading" :data="purchaseInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司名称" align="center" prop="factoryName" />
      <el-table-column label="公司代码" align="center" prop="factoryCode" />
      <el-table-column label="采购人" align="center" prop="cgName" />
      <el-table-column label="要货单位" width="180" show-overflow-tooltip align="center" prop="yhCompany" />
      <el-table-column label="采购单号" width="140" align="center" prop="danhao" />
      <el-table-column label="计划单号" width="150" align="center" prop="yhDanhao" />
      <el-table-column label="项目订单号" width="130" align="center" prop="asnDanhao" />
      <el-table-column label="物料编号" width="130" align="center" prop="itemId" />
      <el-table-column label="子图号" width="130" align="center" prop="itemCode" />
      <el-table-column label="物料名称" width="130" show-overflow-tooltip align="center" prop="itemName" />
      <el-table-column label="规格型号" width="220" show-overflow-tooltip align="center" prop="spec" />
      <el-table-column label="物料单位" align="center" prop="unit" />
      <el-table-column label="供应商名称"  width="130" show-overflow-tooltip align="center" prop="supplyName" />
      <el-table-column label="采购单价" align="center" prop="unitPrice" />
      <el-table-column label="要货数量" align="center" prop="yhNum" />
      <el-table-column label="采购总价" align="center" prop="totalPrice" />
      <el-table-column label="订单数量" align="center" prop="yddNum" />
      <el-table-column label="往采数量" align="center" prop="shNum" />
      <el-table-column label="包装数量" align="center" prop="packingNum" />
      <el-table-column label="超出数量" align="center" prop="jhUnitPrice" />
      <el-table-column label="入库数量" align="center" prop="rkNum" />
      <el-table-column label="供应商编号" width="130" align="center" prop="supplyId" />
      <el-table-column label="合同编号" width="140" align="center" prop="contractCode" />
      <el-table-column label="物料类型编号" width="130" align="center" prop="classId" />
      <el-table-column label="物料类型名称" width="130" show-overflow-tooltip align="center" prop="className" />
      <el-table-column label="要货类型" align="center" show-overflow-tooltip prop="yhType" />
      <el-table-column label="要货时间" align="center" prop="yhTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.yhTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import { listPurchaseInfoSp, getPurchaseInfoSp, delPurchaseInfoSp, addPurchaseInfoSp, updatePurchaseInfoSp,shenpiPurchaseInfo,htScPurchaseInfo} from "@/api/business/purchaseInfoSp";
  export default {
    dicts: ['pur_type','sp_flag'],
    name: "PurchaseInfoSp",
    data() {
      return {
        dhs:[],
        seles:[],
        cgForm:{},
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 采购单表格数据
        purchaseInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        shenh:{
          title: "",
          // 是否显示弹出层
          open: false,
        },
        ht:{
          title: "",
          // 是否显示弹出层
          open: false,
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          danhao: null,
          yhDanhao: null,
          asnDanhao: null,
          classId: null,
          itemName: null,
          itemId: null,
          spec: null,
          supplyId: null,
          contractCode: null,
          shenhFlag: null,
        },
        info:{},
        // 表单参数
        form: {
        },
        // 表单校验
        rules: {
          yhCompany: [
            { required: true, message: "要货单位不能为空", trigger: "blur" }
          ],
          cgName: [
            { required: true, message: "采购人不能为空", trigger: "blur" }
          ],
          /*  yhType: [
              { required: true, message: "请选择要货类型", trigger: "blur" }
            ],*/
          yhTime: [
            { required: true, message: "要货时间不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      //this.getList();
    },
    methods: {
      printCgd(){
        let arr = this.dhs
        if(new Set(arr).size > 1){
          this.$message.error('所选采购单号不同！！！');
        }else{
          this.cgForm = this.seles[0]
          var date = new Date() // 获取时间
          var year = date.getFullYear() // 获取年
          var month = date.getMonth() + 1 // 获取月
          var strDate = date.getDate() // 获取日
          this.cgForm.cgDate = year +'-' +this.getNum(month) +'-' +this.getNum(strDate)
          this.$refs.cghttm1.innerHTML = ''
          new QRCode(this.$refs.cghttm1, {
            text: this.cgForm.danhao, //二维码内容
            width: 80,
            height: 80,
          })
          this.$nextTick(()=>{

            console.log(this.seles[0])
            this.priDan2()
          })

        }
      },
      printSpd(){
        let arr = this.dhs
        if(new Set(arr).size > 1){
          this.$message.error('所选采购单号不同！！！');
        }else{
          this.cgForm = this.seles[0]
          var date = new Date() // 获取时间
          var year = date.getFullYear() // 获取年
          var month = date.getMonth() + 1 // 获取月
          var strDate = date.getDate() // 获取日
          this.cgForm.cgDate = year +'-' +this.getNum(month) +'-' +this.getNum(strDate)
          this.$refs.cghttm2.innerHTML = ''
          new QRCode(this.$refs.cghttm2, {
            text: this.cgForm.danhao, //二维码内容
            width: 80,
            height: 80,
            // colorDark: "#333333", //二维码颜色
            // colorLight: "#ffffff", //二维码背景色
          })
          this.$nextTick(()=>{

            console.log(this.seles[0])
            this.priDan3()
          })

        }
      },
      getNum(i) {
        return i < 10 ? '0' + i : i
      },

      priDan2(){
        // this.$printPage(this.$refs.printDan2)
        const style = '@page { margin:10px 0 0 0 } ' +'@media print { #title{ font-size:12px};#title1{ font-size:12px };.name{font-size:10px}  }' ;
        printJS({
          printable:this.$refs.cght,
          type:'html',
          header:'',
          targetStyles:['*'],
          scanStyles: false,
          style:style,
          onPrintDialogClose: () => {
            console.log('弹窗关闭');
          },
          onLoadingEnd:()=>{
            console.log('加载完毕')
          }
        })
      },
      priDan3(){
        // this.$printPage(this.$refs.printDan2)
        const style = '@page { margin:10px 0 0 0 } ' +'@media print { #title{ font-size:12px};#title1{ font-size:12px };.name{font-size:10px}  }' ;
        printJS({
          printable:this.$refs.cgsqd,
          type:'html',
          header:'',
          targetStyles:['*'],
          scanStyles: false,
          style:style,
          onPrintDialogClose: () => {
            console.log('弹窗关闭');
          },
          onLoadingEnd:()=>{
            console.log('加载完毕')
          }
        })
      },
      tableRowClassName({row, rowIndex}) {
        if (row.shenhFlag == 0) {
          return 'warning-row';
        } else{
          return '';
        }

      },
      /** 查询采购单列表 */
      getList() {
        this.loading = true;
        //this.queryParams.shenhFlag='1'
        listPurchaseInfoSp(this.queryParams).then(response => {
          this.purchaseInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getViewList(danhan){
        this.loading = true;
        this.queryParams.danhao=danhan;
        listPurchaseInfoSp(this.queryParams).then(response => {
          this.purchaseInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          factoryName: null,
          factoryCode: null,
          danhao: null,
          yhDanhao: null,
          asnDanhao: null,
          classId: null,
          className: null,
          itemName: null,
          itemId: null,
          itemCode: null,
          spec: null,
          unit: null,
          txm: null,
          supplyId: null,
          supplyName: null,
          makerCode: null,
          makerName: null,
          contractCode: null,
          contractNum: null,
          contractShare: null,
          totalPrice: null,
          unitPrice: null,
          isTuoguan: null,
          yhNum: null,
          shNum: null,
          rkNum: null,
          hgNum: null,
          thNum: null,
          yhType: null,
          yhCompany: null,
          wlCompany: null,
          dhCompany: null,
          yhTime: new Date(),
          dhStock: null,
          shName: null,
          shKuwei: null,
          shAddress: null,
          ghCompany: null,
          ghDate: null,
          ghAddress: null,
          scDate: null,
          gysYsjl: null,
          gysYssj: null,
          yddNum: null,
          ywcNum: null,
          yghwcbl: null,
          dxrl: null,
          ncgPlanId: null,
          ncgPlanNum: null,
          jcgPlanId: null,
          jcgPlanNum: null,
          ycgPlanId: null,
          ycgPlanNum: null,
          lscgPlanId: null,
          lscgPlanNum: null,
          size: null,
          singleWeight: null,
          totalWeight: null,
          rukuDate: null,
          upLine: null,
          downLine: null,
          stockSafe: null,
          useState: null,
          color: null,
          figure: null,
          caizhi: null,
          isDaizhi: null,
          packingNum: null,
          packingSize: null,
          packingCaizhi: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.seles = selection
        this.dhs = selection.map(item => item.danhao)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "申请采购单";
        setTimeout(() => {
          this.$refs.purchaseInfoAdd.purchaseInfoAddListReset();
        }, 10);
      },
      printt(){
        this.reset();
        const id = this.ids
        getPurchaseInfoSp(id).then(res=>{
          console.log(res,'fffffffffff')
          this.info = res.data
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getPurchaseInfoSp(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改采购单";
          setTimeout(() => {
            this.$refs.purchaseInfoAdd.purchaseInfoEditListReset(response.data);
          }, 10);
        });
      },
      /** 采购单审批 */
      handleShenPi(row) {
        this.reset();
        const id = row.id
        this.shenh.open = true;
        this.shenh.title = "采购单审批";
        this.form.id = id;
        this.form.shenhState="1";
        this.form.shenhFlag="1";
        this.form.shenhTime=new Date();
      },
      /** 采购单审批 */
      handleHt(row) {
        this.reset();
        const id = row.id
        const yhNum = row.yhNum
        this.ht.open = true;
        this.ht.title = "生成采购合同";
        this.form.id = id;
        this.form.contractNum=yhNum;
      },
      submitShenPiForm() {
        shenpiPurchaseInfo(this.form).then(response => {
          this.$modal.alertSuccess("审批成功");
          this.shenh.open = false;
          this.getList();
        });
        /* this.$refs["form"].validate(valid => {
           if (valid) {
             if (this.form.id != null) {
               shenpiPurchaseInfo(this.form).then(response => {
                 this.$modal.alertSuccess("审批成功");
                 this.shenh.open = false;
                 this.getList();
               });
             }
           }
         });*/
      },
      /*生成合同提交*/
      submitHtForm() {
        htScPurchaseInfo(this.form).then(response => {
          this.$modal.alertSuccess("合同生成成功");
          this.ht.open = false;
          this.getList();
        });
        /*this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              htScPurchaseInfo(this.form).then(response => {
                this.$modal.alertSuccess("合同生成成功");
                this.shenh.open = false;
                this.getList();
              });
            }
          }
        });*/
      },

      /** 提交按钮 */
      submitForm() {
        let purchaseInfoAddList= this.$refs.purchaseInfoAdd.purchaseInfoAddList;
        if(purchaseInfoAddList.length==0){
          this.$modal.alertWarning('请选择物料')
          return;
        }
        for(let i=0;i<purchaseInfoAddList.length;i++){
          if(purchaseInfoAddList[i].yhNum==''||purchaseInfoAddList[i].yhNum==null||purchaseInfoAddList[i].yhNum==undefined ||purchaseInfoAddList[i].yhNum<=0){
            this.$modal.alertWarning('请输入采购数量')
            return;
          }
          /*if(purchaseInfoAddList[i].unitPrice==''||purchaseInfoAddList[i].unitPrice==null||purchaseInfoAddList[i].unitPrice==undefined|| purchaseInfoAddList[i].unitPrice<=0){
            this.$modal.alertWarning('请输入采购单价')
            return;
          }*/
        }
        this.form.infoList=JSON.parse(JSON.stringify(purchaseInfoAddList));
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updatePurchaseInfoSp(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPurchaseInfoSp(this.form).then(response => {
                this.$modal.msgSuccess("申请提交成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除采购单编号为"' + ids + '"的数据项？').then(function() {
          return delPurchaseInfoSp(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('business/purchaseInfo/export', {
          ...this.queryParams
        }, `purchaseInfo_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
<style lang="scss" scoped>
  ::v-deep .el-table .warning-row {
    background: #ffe4e1;
  }
</style>
